package com.tanhua.server.controller;


import com.tanhua.model.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.SmallVideosService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频用户点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String videoId) {
        Long userId = UserHolder.getUserId();
        videosService.likeVideoId(userId,videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String videoId) {
        Long userId = UserHolder.getUserId();
        videosService.disLikeVideoId(userId,videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity userFocus(@PathVariable("id") String videoId) {
        Long userId = UserHolder.getUserId();
        videosService.loveVideoId(userId,videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频取消用户关注
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("id") String videoId) {
        Long userId = UserHolder.getUserId();
        videosService.unloveVideoId(userId,videoId);
        return ResponseEntity.ok(null);
    }

    //视频评论-提交
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@PathVariable("id") ObjectId videoId, @RequestBody Map map) {
        String comment = (String)map.get("comment");
        videosService.saveComments(videoId,comment);
        return ResponseEntity.ok(null);
    }

    //视频评论点赞
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable("id") ObjectId videoId){
        Long userId = UserHolder.getUserId();
        videosService.commentsLike(videoId,userId);
        return ResponseEntity.ok(null);
    }

    //视频取消评论点赞
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsdisLike(@PathVariable("id") ObjectId videoId){
        Long userId = UserHolder.getUserId();
        videosService.commentsdisLike(videoId,userId);
        return ResponseEntity.ok(null);
    }
    //视频评论列表
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1")  Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult result = videosService.queryComments(page, pagesize);
        return ResponseEntity.ok(result);
    }


}